package com.exp.controller;

import com.common.controller.BaseController;
import com.common.controller.Constant;
import com.exp.model.Admin;
import com.exp.model.Business;
import com.exp.model.User;
import com.exp.service.AdminService;
import com.exp.service.BusinessService;
import com.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 Created by guojianyu on 2016/3/3.
 */
@Controller
@RequestMapping("/login")
public class LoginAuthentiaction {
 @Autowired
 private UserService userService;
 @Autowired
 private AdminService adminService;
 @Autowired
 private BusinessService businessService;
 @RequestMapping("/userLogin")
 @ResponseBody
 public boolean userLogin(@RequestBody Map map,HttpServletRequest request){
  List<User> list=userService.userLogin(map);
  HttpSession session=request.getSession();
  session.setAttribute(Constant.SESSION_CURRENT_USER,list.get(0));
  if(list.size()>0){
   return true;
  }else{
   return false;
  }
 }
 @RequestMapping("/adminLogin")
 @ResponseBody
 public boolean adminLogin(@RequestBody Map map,HttpServletRequest request){
  List<Admin> list=adminService.adminLogin(map);
  HttpSession session=request.getSession();
  session.setAttribute(Constant.SESSION_CURRENT_ADMIN,list.get(0));
  if(list.size()>0){
   return  true;
  }else{
   return false;
  }
 }
 @RequestMapping("/businessLogin")
 @ResponseBody
 public boolean businessLogin(@RequestBody Map map,HttpServletRequest request,
                              HttpServletResponse response){
//  获取list为了获取多个对象（重名情况） 也可以获取实体bean （只能获取单个对象）
  List<Business> list = businessService.businessLogin(map);
//  创建一个session
  HttpSession session = request.getSession();
//  通过当前登陆的用户名密码查询出来的list放到session对象里
//  baseController 实现查询当前商家id
  session.setAttribute(Constant.SESSION_CURRENT_BUSINESS,list.get(0));
/*  for(int i=0;i<list.size();i++){
   String status=list.get(i).getStatus();
  }*/
  if (list.size()>0) {
    return true;
  } else {
   return false;
  }
 }
 /*
// 通过实体bean来接
 @RequestMapping("/businessLogin")
 @ResponseBody
 public boolean businessLogin(@RequestBody Map map,HttpServletRequest request,
                              HttpServletResponse response){
  Business business = businessService.businessLogin(map);
  HttpSession session = request.getSession();
  session.setAttribute(Constant.SESSION_CURRENT_BUSINESS,business);
  if (business.getId()>0) {
   return true;
  } else {
   return false;
  }
 }*/
}
